package com.isoftstone.app.controller.partyMemberShipDues;

import com.isoftstone.bank.controller.base.BaseController;
import com.isoftstone.common.Tidings;
import com.isoftstone.merchant.entity.merchant.Merchant;
import com.isoftstone.merchant.entity.mproject.MProject;
import com.isoftstone.merchant.entity.payment.Payment;
import com.isoftstone.merchant.service.merchant.impl.MerchantServiceImpl;
import com.isoftstone.merchant.service.mproject.impl.MProjectServiceImpl;
import com.isoftstone.merchant.service.payment.impl.PaymentServiceImpl;
import com.isoftstone.util.Const;
import com.isoftstone.util.PropertiesUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: zhaojg
 * @Description
 * @Date: Created in 9:46 2017/12/18
 * @Modified by:
 * <p>
 * 缴纳党费接口
 */
@Controller
public class memberShipController extends BaseController {

    @Autowired
    MerchantServiceImpl merchantService;

    @Autowired
    MProjectServiceImpl mProjectService;

    @Autowired
    PaymentServiceImpl paymentService;

    /**
     * @param openAccount 中银易商账号
     * @param cityCode    城市id
     * @return 有缴纳党费项目的缴费单位
     */
    @RequestMapping(value = "app/admin/queryMemberShip", method = RequestMethod.POST)
    @ResponseBody
    public Object queryMemberShipMerchant(String openAccount, String cityCode) {
        Tidings<Object> tidings = new Tidings<Object>();
        List<Merchant> memberShipMerchant = null;
        try {
            memberShipMerchant = merchantService.getMemberShipMerchant(openAccount, cityCode);
            if(memberShipMerchant.size() > 0){
                for (Merchant merchant : memberShipMerchant) {
                    String logoFile = merchant.getLogoFile();
                    if (logoFile != null) {
                        String path = PropertiesUtils.getProperties("baseContextPath") + Const.FILEPATHIMG + logoFile;
                        merchant.setLogoFile(path);
                    }
                }
                tidings.setMsg("查询成功");
                tidings.setStatus(Const.SUCCESS);
                tidings.setT(memberShipMerchant);
            }else{
                tidings.setMsg("该缴费地区下没有缴费单位");
                tidings.setStatus(Const.MYJFDW);
                tidings.setT(memberShipMerchant);
            }
        } catch (Exception e) {
            tidings.setMsg("查询失败");
            tidings.setStatus(Const.ERROR);
            e.printStackTrace();
        }
        return tidings;
    }
    /**
     * 根据缴费单位id查询有缴纳党费的缴费项目
     *
     * @param corpId
     * @param openAccount
     * @return
     */
    @RequestMapping(value = "app/admin/queryMemberShipProject", method = RequestMethod.POST)
    @ResponseBody
    public Object queryMemberProject(String corpId,String openAccount) {
//        corpId = "41";
        Tidings<Object> tidings = new Tidings<Object>();
        try {
            List<MProject> memberShipProject = mProjectService.getMemberShipProject(corpId,openAccount);
            if(memberShipProject.size() > 0){
                tidings.setMsg("查询成功");
                tidings.setStatus(Const.SUCCESS);
                tidings.setT(memberShipProject);
            }else{
                tidings.setMsg("该缴费地区下没有缴费项目");
                tidings.setStatus(Const.MYJFXM);
                tidings.setT(memberShipProject);
            }
        } catch (Exception e) {
            tidings.setMsg("查询失败");
            tidings.setStatus(Const.ERROR);
            e.printStackTrace();
        }
        return tidings;
    }

    /**
     * @param corpId    缴费单位id
     * @param projectId 缴费项目id
     * @param feeId     缴费编号
     * @return
     */
    @RequestMapping(value = "app/admin/queryMemberShipProjectDetail", method = RequestMethod.POST)
    @ResponseBody
    public Object queryMemberProjectDetail(String corpId, String projectId, String feeId) {

        Tidings<Map<String, Object>> tidings = new Tidings<Map<String, Object>>();
        Map<String, Object> reMap = new HashMap<String, Object>();
        String status = Const.DJF;
        //根据缴费项目project_id和缴费编号和corpId查询
        List<Payment> paymentList = null;
        try {
            paymentList = paymentService.queryMemberShipDetail(corpId, projectId, feeId);
            //如果没有查到，则提示用户不存在
            if (paymentList.size() == 0) {
                tidings.setMsg("用户不存在");
                tidings.setStatus(Const.YHBCZ);
                return tidings;
            }
            long count = paymentList.stream().filter(payment -> payment.getPayStatus().equals(Const.YJF)).count();
            if (paymentList.size() == count) {
                tidings.setMsg("您当前没有未缴费项目");
                tidings.setStatus(Const.MYJFXM);
                return tidings;
            } else {
                List<Payment> paymentList1 = paymentService.queryMemberShipDetailByStatus(corpId, projectId, feeId, status);
                reMap.put("paymentList1", paymentList1);
                tidings.setMsg("有缴费项目");
                tidings.setStatus(Const.YJFXM);
                tidings.setT(reMap);
                return tidings;
            }
        } catch (Exception e) {
            tidings.setMsg("查询失败");
            tidings.setStatus(Const.ERROR);
            e.printStackTrace();
        }
        return tidings;
    }

}
